//
//  HMShareManager+Extension.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/5/4.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import Foundation
//封装新浪微博网络请求方法
extension HMShareManager{
    
    //在这里需要把下拉刷新since_id 和上拉刷新max_id参数以及参数类型传到服务器
    func statusList(since_id:Int64 = 0,max_id:Int64 = 0,completion:@escaping (_ list:[[String:Any]]?,_ isSuccess:Bool) -> ()) {
        //这个是home页面的接口
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id":"\(since_id)",
                      "max_id":"\(max_id > 0 ? max_id - 1 :0)"]
        
        tokenRequest( urlString: urlString, parameters:params) { (responseObj, isSuccess) in
            
            print(responseObj ?? 0)
            //从responseObj statuses 获取字典数组
            let result = responseObj?["statuses"] as? [[String:Any]]
            
            completion(result, isSuccess)
        }
    }
    //返回微博的未读数量0
    func unreadCount(completion:@escaping(_ count:Int) -> ()){
        //这个是提醒接口
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
       
        
        guard let uid = userAccount.uid else{
            return
        }
        let params = ["uid":uid]
        tokenRequest(urlString: urlString, parameters: params) { (responseObj, isSuccess) in
            print(responseObj ?? 0)
            
            let dict = responseObj
            
            let count = dict?["status"] as? Int
            
            completion(count ?? 0)
            
        }
    }
    
}
//MARK:用户信息
extension HMShareManager{
    //加载当前用户信息 用户登录后立即执行
    func loadUserInfo(completion:@escaping(_ dict:[String:Any])->())  {
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        guard let uid = userAccount.uid else{
            return
        }
        let params = ["uid":uid]
        //发起网络请求
        tokenRequest(requestType: .GET, urlString: urlString, parameters: params) { (json, isSuccess) in
            
            completion(json ?? [:])
        }
    }
    
}
extension HMShareManager{
    //加载请求
    
    /// accesstoken
    ///- Parameter completion:请求回调闭包
    /// - Parameter code: code
    ///异步加载网络请求 需要参数执行回调
    func loadAccessToken(code:String,completion:@escaping (_ isSuccess:Bool) -> ()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":HMAppKey,"client_secret":HMAppSecret,"grant_type":"authorization_code","code":code,"redirect_uri":HMRedirectUrl]
        
    //发起请求 这里需要POST请求
       request(requestType: .POST, urlString: urlString, parameters: params) { (json, isSuccess) in
        print(json ?? "")
         //这里无论是否加载数据成功 对用户不会有任何影响
         //使用yy_modelSet设置字典转模型 类似于KVC 但是他直接处理了没有属性崩溃的情况
        self.userAccount.yy_modelSet(with: (json) ?? [:])
       
        
        //加载用户信息
        self.loadUserInfo(completion: { (dict) in
            
            print(dict)
            
            //使用用户字典设置用户信息(昵称和头像地址)
               self.userAccount.yy_modelSet(with: dict)
            
            //保存模型
            self.userAccount.saveAccount()
            
            
            print(self.userAccount)
            
              //用户信息加载完城 完成回调
             completion(isSuccess)
        })
        
       
        
        }
    }
}
